﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;

namespace RTF2XML
{
    public class PlainText : GroupItem
    {
        public string sText = "";

        /// <summary>
        /// Creates a new plain text control with the input text
        /// </summary>
        /// <param name="sText"></param>
        public PlainText(string sText)
        {
            this.sText = sText;
        }

        /// <summary>
        /// Adds the control to the xml parent node in sequence with the other nodes
        /// </summary>
        /// <param name="xmlParentNode"></param>
        public override void AddXML(ref XmlElement xmlParentNode)
        {
            int iSeqNum = 0;

            if (xmlParentNode.HasChildNodes)
            {
                //Get the last sequence number of the other children nodes
                string seqNum = xmlParentNode.LastChild.Attributes["seq"].Value;
                if (int.TryParse(seqNum, out iSeqNum))
                {
                    //Increment the sequence number
                    iSeqNum++;
                }
                else
                {
                    throw new Exception("Sequence number is not valid");
                }
            }

            //Add a new element for the control
            XmlElement child = xmlParentNode.OwnerDocument.CreateElement("PlainText");
            //Add the sequence number
            child.SetAttribute("seq", iSeqNum.ToString());
            //Set the inner text of the element to the plain text
            child.InnerText = this.sText;
            //Add the element to the parent node
            xmlParentNode.AppendChild(child);
        }
    }
}
